/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "../../stdafx.h"
#include "../../include/div/FCMenuItem.h"

namespace FaceCat{
	FCMenuItem::FCMenuItem(){
		m_checked = false;
		m_dropDownMenu = 0;
		m_parentItem = 0;
		m_parentMenu = 0;
		FCFont font(L"Default", 12, false, false, false);
		setFont(&font);
		FCSize size = {200, 25};
		m_size = size;
	}

	FCMenuItem::FCMenuItem(const String& text){
		m_checked = false;
		m_dropDownMenu = 0;
		m_parentItem = 0;
		m_parentMenu = 0;
		FCFont font(L"Default", 12, false, false, false);
		setFont(&font);
		FCSize size = {200, 25};
		m_size = size;
		setText(text);
	}

	FCMenuItem::~FCMenuItem(){
		clearItems();
		m_dropDownMenu = 0;
		m_parentItem = 0;
		m_parentMenu = 0;
	}

	bool FCMenuItem::isChecked(){
		return m_checked;
	}

	void FCMenuItem::setChecked(bool value){
		m_checked = value;
	}

	FCMenu* FCMenuItem::getDropDownMenu(){
		return m_dropDownMenu;
	}

	void FCMenuItem::setDropDownMenu(FCMenu *value){
		m_dropDownMenu = value;
	}

	FCMenuItem* FCMenuItem::getParentItem(){
		return m_parentItem;
	}

	void FCMenuItem::setParentItem(FCMenuItem *value){
		m_parentItem = value;
	}

	FCMenu* FCMenuItem::getParentMenu(){
		return m_parentMenu;
	}

	void FCMenuItem::setParentMenu(FCMenu *value){
		m_parentMenu = value;
	}

	String FCMenuItem::getValue(){
		return m_value;
	}

	void FCMenuItem::setValue(const String& value){
		m_value = value;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	void FCMenuItem::addItem(FCMenuItem *item){
		item->setParentItem(this);
		item->setParentMenu(m_parentMenu);
        item->onAddingItem(-1);
        m_items.add(item);
	}

	void FCMenuItem::clearItems(){
	    ArrayList<FCMenuItem*> itemsCopy;
        int itemSize = (int)m_items.size();
        for (int i = 0; i < itemSize; i++){
            itemsCopy.add(m_items.get(i));
        }
        int copySize = (int)itemsCopy.size();
        for (int i = 0; i < copySize; i++){
            itemsCopy.get(i)->onRemovingItem();
			delete itemsCopy.get(i);
        }
        m_items.clear();
	}

	String FCMenuItem::getViewType(){
		return L"MenuItem";
	}

	ArrayList<FCMenuItem*> FCMenuItem::getItems(){
		return m_items;
	}

	void FCMenuItem::getAttribute(const String& name, String *value, String *type){
		if(name == L"checked"){
			*type = L"bool";
			*value = FCTran::boolToStr(isChecked());
		}
		else if(name == L"value"){
			*type = L"text";
			*value = getValue();
		}
		else{
			FCButton::getAttribute(name, value, type);
		}
	}

	ArrayList<String> FCMenuItem::getAttributeNames(){
		ArrayList<String> attributeNames = FCButton::getAttributeNames();
		attributeNames.add(L"Checked");
		attributeNames.add(L"Value");
		return attributeNames;
	}

	void FCMenuItem::insertItem(int index, FCMenuItem *item){	
		item->setParentItem(this);
        item->setParentMenu(m_parentMenu);
        item->onAddingItem(index);
        m_items.insert(index, item);
	}

	void FCMenuItem::onAddingItem(int index){
		FCMenu *dropDownMenu = 0;
        if (m_parentItem){
            dropDownMenu = m_parentItem->getDropDownMenu();
            if (!dropDownMenu){
                dropDownMenu = m_parentMenu->createDropDownMenu();
                dropDownMenu->setVisible(false);
                m_parentItem->setDropDownMenu(dropDownMenu);
				dropDownMenu->setParentItem(m_parentItem);
                m_parentMenu->getNative()->addView(dropDownMenu);
            }
        }
        else{
            dropDownMenu = m_parentMenu;
        }
        if (dropDownMenu){
            if (index != -1){
                dropDownMenu->insertView(index, this);
            }
            else{
                dropDownMenu->addView(this);
            }
			int itemSize = (int)m_items.size();
            for (int i = 0; i < itemSize; i++){
                m_items.get(i)->onAddingItem(-1);
            }
        }
	}

	void FCMenuItem::onClick(FCTouchInfo touchInfo){
		FCButton::onClick(touchInfo);
        if (m_parentMenu){
            m_parentMenu->onMenuItemClick(this, touchInfo);
        }
	}

	void FCMenuItem::onTouchMove(FCTouchInfo touchInfo){
		FCButton::onTouchMove(touchInfo);
        if (m_parentMenu){
            m_parentMenu->onMenuItemTouchMove(this, touchInfo);
        }
	}

	void FCMenuItem::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
        int width = getWidth(), height = getHeight();
		if(width > 0 && height > 0){
			int right = width;
			int midY = height / 2;
			String text = getText();
			int tRight = 0;
			Long textColor = getPaintingTextColor();
			if ((int)text.length() > 0){
				FCFont *font = getFont();
				FCSize tSize = paint->textSize(text, font, -1);
				FCRect tRect = {0};
				tRect.left = 10;
				tRect.top = midY - tSize.cy / 2;
				tRect.right = tRect.left + tSize.cx;
				tRect.bottom = tRect.top + tSize.cy;
				paint->drawText(text, textColor, font, tRect, -1);
				tRight = tRect.right + 4;
			}
			if (m_checked){
				FCRect eRect = {tRight, height / 2 - 4, tRight + 8, height / 2 + 4};
				paint->fillEllipse(textColor, eRect);
			}
			if(m_items.size() > 0){
				FCPoint point1 = {0}, point2 = {0}, point3 = {0};
				FCMenu *menu = m_parentMenu;
				if (m_parentItem){
					menu = m_parentItem->getDropDownMenu();
				}
				FCLayoutStyle layoutStyle = menu->getLayoutStyle();
				if (layoutStyle == FCLayoutStyle_LeftToRight || layoutStyle == FCLayoutStyle_RightToLeft){
					point1.x = right - 25;
					point1.y = midY - 2;
					point2.x = right - 14;
					point2.y = midY - 2;
					point3.x = right - 20;
					point3.y = midY + 4;
				}
				else{
					point1.x = right - 15;
					point1.y = midY;
					point2.x = right - 25;
					point2.y = midY - 5;
					point3.x = right - 25;
					point3.y = midY + 5;
				}
				FCPoint points[3];
				points[0] = point1;
				points[1] = point2;
				points[2] = point3;
				paint->fillPolygon(textColor, points, 3);
			}
		}
	}

	void FCMenuItem::onRemovingItem(){
	    FCMenu *dropDownMenu = 0;
        if (m_parentItem){
            dropDownMenu = m_parentItem->getDropDownMenu();
        }
        else{
            dropDownMenu = m_parentMenu;
        }
		if (dropDownMenu){
			int itemSize = (int)m_items.size();
			for (int i = 0; i < itemSize; i++){
				m_items.get(i)->onRemovingItem();
			}
			dropDownMenu->removeView(this);
		}
		if (m_dropDownMenu){
            m_parentMenu->getNative()->removeView(m_dropDownMenu);
            delete m_dropDownMenu;
            m_dropDownMenu = 0;
        }
	}

	void FCMenuItem::removeItem(FCMenuItem *item){
		item->onRemovingItem();
		for(int i = 0; i < m_items.size(); i++){
			if(item == m_items.get(i)){
				m_items.removeAt(i);
				return;
			}
		}
	}

	void FCMenuItem::setAttribute(const String& name, const String& value){
		if(name == L"checked"){
			setChecked(FCTran::strToBool(value));
		}
		else if(name == L"value"){
			setValue(value);
		}
		else{
			FCButton::setAttribute(name, value);
		}
	}
}